home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacHack 1996
/
MacHack 1996.toast
/
Presentations
/
Presentations ’94
/
Timothy Knox
/
Pocket6.3
/
Examples
/
TextEdit
< prev
next >
Wrap
Text File
|
1994-06-24
|
9KB
|
187 lines
( text edit example for Pocket Forth 0.6 or 1.6 18:06 6/9/91 )
forget task : task ; decimal
page 0 28 +md ! ( kill echo )
( text edit record handle )
2variable TERECORD ( pronounced "terra chord" )
: TEH ( -- dhandle ) terecord 2@ ; ( the TE record handle )
( te toolbox routines )
: TENEW ( -- ) ( initialize the TE record )
0 0 2>r ( room for result from toolbox function )
4 +md a>r ( push dest rect address to rstack )
4 +md a>r ( " view " " " " )
,$ A9D2 2r> ( _TENew then pop handle from rstack )
terecord 2! ; ( store the handle away for later )
: TESETTEXT ( addr len -- ) ( set text to string from stack )
swap a>r ( push string address to rstack )
s>d 2>r ( push 32 bit string length to rstack )
teh 2>r ( push the terecord's handle to rstack )
,$ A9CF ; ( _TESetText )
: TEGETTEXT ( -- dhandle ) ( get a handle to the text )
0 0 2>r ( room for the text handle )
teh 2>r ( push the terecord's handle to rstack )
,$ A9CB 2r> ; ( _TEGetText, pop handle from rstack )
: TELENGTH ( -- n ) ( get the length of the text )
teh dl@ ( get pointer to the text )
60 s>d d+ ( add teLength offset to pointer )
l@ ; ( fetch length value )
: TECLICK ( -- ) ( handle a click in the TE's rect )
@mouse 2>r ( push the _current_ mouse position to rstack )
0 >r ( not an extended click )
teh 2>r ( push the terecord's handle to rstack )
,$ A9D4 ; ( _TEClick )
: TEKEY ( c -- ) ( handle a character from the stack )
>r ( push the character to the rstack )
teh 2>r ,$ A9DC ; ( push handle _TEKey )
: TEUPDATE ( -- ) ( draw the editable text )
4 +md a>r ( push the view rect's address to rstack )
teh 2>r ,$ A9D3 ; ( push handle _TEUpdate )
: TEACTIVATE ( -- ) ( show selection, etc. )
teh 2>r ,$ A9D8 ; ( push handle _TEActivate )
: TEDEACTIVATE ( -- ) ( hide selection, etc. )
teh 2>r ,$ A9D9 ; ( push handle _TEActivate )
: TEIDLE ( -- ) ( blink the cursor )
teh 2>r ,$ A9DA ; ( push handle _TEIdle )
: TECUT ( -- ) teh 2>r ,$ A9D6 ; ( push handle _TECut )
: TECOPY ( -- ) teh 2>r ,$ A9D5 ; ( push handle _TECopy )
: TEPASTE ( -- ) teh 2>r ,$ A9DB ; ( push handle _TEPaste )
: TEDISPOSE ( -- ) teh 2>r ,$ A9CD ; ( push handle _TEDispose )
( private te scrap to clipboard conversion )
: "TEXT" ( -- d'TEXT' ) [ 22612 21573 dliteral ] ; macro
: TEFROMSCRAP ( -- ) ( move clipboard contents to TE scrap )
0 0 2>r ( room on rstack for toolbox function result )
2740 0 dl@ 2>r ( push TEScrpHandle to rstack )
"text" 2>r ( scrap type identifier )
here a>r ( here is used as a temporary variable )
,$ A9FD ( _GetScrap )
2r> 0< IF ( just test the high byte )
drop beep ( drop error code & beep )
ELSE 2736 0 l! THEN ; ( set TEScrpLength )
: TETOSCRAP ( -- ) ( move TE scrap to clipboard )
0 0 2>r ( room on rstack for toolbox function result )
,$ A9FC ( _ZeroScrap )
2736 0 l@ 0 2>r ( push TEScrpLength to rstack )
"text" 2>r ( scrap type identifier )
2740 0 dl@ dl@ 2>r ( double dereference TEScrpHandle )
,$ A9FE ( _PutScrap )
2r> + IF beep THEN ; ( beep if error )
( activate and edit menu handlers )
: MYACT ( f -- ) IF teactivate ELSE tedeactivate THEN ;
: EDITMENU ( n -- addr ) ( item to address, undo is 0 )
18 +md @ 2+ @ swap 2* + ;
: CUT ( -- ) tecut tetoscrap ;
: COPY ( -- ) tecopy tetoscrap ;
: PASTE ( -- ) tefromscrap tepaste ;
( string compilation )
: EVEN ( n -- n' ) dup 2 mod + ; ( round n up to an even number )
: ," ( -- ) ( compile a quoted string from input stream )
34 word here c@ 1+ even allot ; immediate
( a string )
create INTRO ( -- addr ) ( some text to edit )
," Press 'Enter' to quit, hold option key to save."
: NOCURSOR ( -- ) ( don't draw the little line cursor )
20085 14 +md @ ! ; ( replace cursor routine with RTS )
: !EDIT ( -- ) ( set input routines to edit text )
nocursor page ( prepare the window )
[ ' teclick literal ] 16 +md ! ( set button handler )
[ ' teidle literal ] 20 +md ! ( set idle handler )
[ ' teupdate literal ] 14 +md ! ( set update handler )
[ ' myact literal ] 12 +md ! ( set activate handler )
[ ' cut literal ] 2 editmenu ! ( set cut )
[ ' copy literal ] 3 editmenu ! ( set copy )
[ ' paste literal ] 4 editmenu ! ( set paste )
intro count tesettext ; ( set the initial text to edit )
: !INTERPRET ( -- ) ( reset the interpreter handlers )
[ ' beep literal ] 16 +md ! ( reset button handler )
[ ' null literal ] 20 +md ! ( reset idle handler )
[ 14 +md @ literal ] 14 +md ! ( reset update )
[ ' drop literal ] 12 +md ! ( reset activate )
[ ' beep literal ] 2 editmenu ! ( reset cut )
[ ' beep literal ] 3 editmenu ! ( reset copy )
[ 4 editmenu @ literal ] 4 editmenu ! ( reset paste )
[ 14 +md @ @ literal ] 14 +md @ ! ; ( reset cursor )
( This part is from the Release 4 file "DataFiles". )
variable FCB 78 allot ( the file control block )
: +FCB ( offset -- addr ) fcb + ; ( offset into fcb )
: 0FCB ( -- ) fcb 80 0 fill ; ( clear the fcb )
: FTRAP ( -- ) fcb >abs ,$ 205E ; ( movea.l [ps]+,a0 )
: CLOSE ( -- ) ftrap ,$ A001 ftrap ,$ A013 ; ( close & flush )
: ?DERROR ( -- ) ( nothing if no error, quit if disk error )
16 +fcb @ ?dup IF ( if result not zero )
." DiskError" . close abort THEN ; ( report & abort )
: !SIZE ( bytes -- ) 38 +fcb ! ; ( set bytes-to-read or write )
: !NAME ( name.addr -- ) >abs 0fcb 18 +fcb 2! ; ( set name )
: !TYPE ( dtype -- ) 32 +fcb 2! ( set the file type )
ftrap ,$ A00D ?derror ; ( _SetFileInfo )
create FILENAME ( -- name.addr ) ," Pocket Text" 54 allot
create PROMPTSTR ( -- addr ) ," Save the text as:"
: NEW ( name.addr -- ) ( create a file, or replace an existing one )
pad 74 0 fill ( clean out pad )
55 75 2>r ( top left corner )
promprstr a>r filename a>r ( prompt and default file name )
0 0 2>r pad a>r ( reply record address [at pad] )
1 >r ,$ a9ea ( _SFPutFile )
pad 10 + !name ( set the file name )
pad 6 + @ 22 +fcb ! ( set vrefnum )
ftrap ,$ A008 ( _Create )
16 +fcb @ -48 = 0= IF ( This line has been added to ... )
?derror THEN ; ( ... ignore duplicate file name errors. )
: OPEN ( -- ) ftrap ,$ A000 ?derror ; ( _Open the file )
: WRITE ( dabs.addr -- ) ( write to file from absolute address )
32 +fcb 2! ( set write buffer pointer )
ftrap ,$ A003 ?derror ; ( _Write )
: SAVETEXT ( -- ) ( save the text to the file )
new open ( create a new file and open it )
"text" !type ( set file type to TEXT )
telength !size ( set the number of bytes to write )
tegettext dl@ write ( send the text to the file )
close ; ( close the file )
( If an I/O error occurs, type: !interpret tedispose )
( event record access / command key test )
: ?DA ( -- flag ) ( true if the DA type is running )
0 +md 2@ ( the window's pointer )
108 0 d+ l@ 0< ; ( the windowKind integer<0 if DA kind )
: +ERECORD ( offset -- dabs.addr ) ( access the event record )
?da IF ( is it the DA )
,$ 2044 ( movea.l d4,a0 ) ( D4 has parameter block address )
,$ 2D28 ,$ 1C ( move.l csParams[a0],-[ps] ) ( push address )
ELSE 148 +md >abs ( address is in +md array )
THEN rot 0 d+ ; ( double.offset + erecord dabs.addr )
: META ( -- n ) 14 +erecord l@ ; ( get meta keys word )
: ?CMD ( -- flag ) meta 256 and ; ( true if clover key is down )
: ?OPTION ( -- flag ) meta 2048 and ; ( true if option key is down )
: COMMANDKEYS ( c -- ) ( do command key handlers )
>r ( hold the character on the return stack )
r 120 = IF cut ELSE ( if character = x then cut )
r 99 = IF copy ELSE ( if character = c then copy )
r 118 = IF paste THEN ( if character = v then paste )
THEN THEN r> drop ; ( pop and drop the character )
: EDIT ( -- ) ( run the demo )
tenew ( create the text edit record )
!edit ( set the text edit event handlers )
teupdate ( draw the existing text )
teactivate ( start editing text )
BEGIN
key dup ( get a key )
3 > WHILE ( until "enter" is pressed )
?cmd IF ( check cmd key )
commandkeys ELSE tekey THEN ( handle key presses )
REPEAT drop
tedeactivate ( turn off text editing )
!interpret ( reset the standard event handlers )
?option IF savetext THEN ( save the text to a file )
tedispose ; ( get rid of the text edit record )
-1 28 +md ! edit